package com.zhiyou100.springbootmybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhiyou100.springbootmybatisplus.mapper.ITeacherMapper;
import com.zhiyou100.springbootmybatisplus.pojo.Teacher;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
public class TeacherController {
    @Resource
    private ITeacherMapper teacherMapper;
    @PostMapping("add")
        public  String add(Teacher teacher){
        int insert = teacherMapper.insert(teacher);
        return insert+"666666666";
    }
    @GetMapping("select")
    public  List<Teacher> query(){
        List<Teacher> teachers = teacherMapper.selectList(null);
        return teachers;
    }
    @GetMapping("find")
    public  Teacher find(Integer id){
        Teacher teachers = teacherMapper.selectById(id);
        return teachers;
    }
    @GetMapping("findName")
    public  Teacher findName(String name){
        QueryWrapper queryWrapper = new QueryWrapper();
        Teacher teacher = new Teacher();
        teacher.setName(name);
        queryWrapper.setEntity(teacher);
       Teacher teachers = teacherMapper.selectOne(queryWrapper);
        return teachers;
    }
    @DeleteMapping("delete")
    public String delete(Integer id){
        System.out.println("delete");
        int i = teacherMapper.deleteById(id);
        return i+"成功";
    }
    @PostMapping("update")
    public  String update(Teacher teacher){
        int update = teacherMapper.updateById(teacher);

        return "hahhaha"+update;
    }
    @PostMapping("updateName")
    public  String updateName(Teacher teacher){
        UpdateWrapper objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.set("name",teacher.getName());
        int update = teacherMapper.update(teacher, objectUpdateWrapper);
        return "hahhaha"+update;
    }

}
